******************************************
* Time preferences and credit constraints 
* Dean & Sautmann 2019
******************************************

//September 14, 2019 - Anja Sautmann, email: sautmann@mit.edu

// Change the path below to data folder
clear all

global datafolder "/Users/Data"

cd $datafolder

use "$datafolder/Panel-TP-final.dta", clear

version 14.2

// *****************************
// household characteristics 
//  *Table 10*

preserve
duplicates drop CO HH, force // week duplicates

sum salaried_job sex age_under age_over edlevel readwrite HHmembers HHkids
restore


// Check heterogeneity in inconsistent choices

foreach demog in salaried_job sex age_u age_o proprietaire readwrite edlevel {
	reg flag_inconsistent `demog', vce(cluster HH)
	}
// Note: proprietaire indicates of the person owns their residence.

// *****************************
// Time preferences
// *Table 3*

// summary stats of MRS 
bysort week: tab flag_Aconsistent
bysort week: tab flag_Bconsistent
bysort week: ///
 sum TPnewA TPnewB CFAA CFAB flag_Anegative flag_Bnegative flag_equal flag_more*

// week by week correlations
bysort week: correlate TPnewA TPnewALag 
bysort week: correlate TPnewB TPnewBLag 

// *Table 11*
// raw distribution of implied MRS by week (table in appendix D)
bysort week: tab TPnewA

// Test: decision B more or less patient than decision A? (no clustering here)
ttest TPnewA==TPnewB // not rejected

// checking sample selection: households with missing weeks
preserve
drop if TPnewA==.
duplicates tag CO HH, gen(all_obs)
tab all_obs
replace all_obs =all_obs-1 if all_obs==1 | all_obs==2

reg Income all_obs, vce(cluster HH)
reg EarningsLabor all_obs, vce(cluster HH)
reg EarningsNonLabor all_obs, vce(cluster HH)
reg EarningsNonLaborEx all_obs, vce(cluster HH)
reg EarningsNonLaborEnd all_obs, vce(cluster HH)
reg Spending all_obs, vce(cluster HH)
reg Savings all_obs, vce(cluster HH)
reg Event all_obs, vce(cluster HH)
reg EventSpend all_obs, vce(cluster HH)

collapse (mean) Income Spending Savings Event EarningsLabor EarningsNonLabor* EventSpend all_obs, by (HH)
ttest Income, by(all_obs)
ttest EarningsLabor, by(all_obs)
ttest EarningsNonLaborEnd, by(all_obs)
ttest EarningsNonLaborEx, by(all_obs)

ttest Spending, by(all_obs)
ttest Savings, by(all_obs)
ttest Event, by(all_obs)
ttest EventSpend, by(all_obs)
restore

// *****************************
// Financial variables
// *Table 4
 
tabstat Income EarningsLabor EarningsNonLabor  EarningsNonLaborEx EarningsNonLaborEnd, stat(min med mean max sd) columns(s)
tabstat Spending SpendNec EventSpend Event Savings, stat(min med mean max sd) columns(s)

// *******************************************
// OLS REGRESSIONS
// *******************************************

// Panel data set:
xtset HH week 
// HH is unique - one person per household

// *******************************************
// *Prediction - Consumption and Income Shocks 
// *******************************************

// **Income, breaking out "endogenous" and 
// "nonendogenous" earnings components (with FE & clustering)


// ** Table 5: income shocks (OLS only; see separate file for conditional logit)
// all income
xi: xtreg TPnewA Income        , fe  cluster(HH)
xi: xtreg TPnewA Income i.week , fe  cluster(HH)   
// split by source
xi: xtreg TPnewA EarningsLabor EarningsNonLaborEnd EarningsNonLaborEx      , fe cluster(HH)
xi: xtreg TPnewA EarningsLabor EarningsNonLaborEnd EarningsNonLaborEx i.week, fe cluster(HH)

// ** Figure 1 (appendix)
// graphs: income distributions conditional on non-zero realization

set scheme s2mono
histogram EarningsNonLaborEx if EarningsNonLaborEx>0 , frequency start(0) bin(40) ///
 xscale(range(0 11)) yscale(range(0 630)) ylabel(200 400 600) graphregion(color(white)) aspectratio(1) xtitle(Exogenous nonlabor income) 
histogram EarningsNonLaborEnd if EarningsNonLaborEnd>0 , frequency start(0) bin(40) ///
 xscale(range(0 11)) yscale(range(0 630)) ylabel(200 400 600) graphregion(color(white)) aspectratio(1) xtitle(Endogenous nonlabor income)
histogram EarningsLabor if EarningsLabor>0 , frequency start(0) bin(40) ///
 xscale(range(0 11)) yscale(range(0 630)) ylabel(200 400 600) graphregion(color(white)) aspectratio(1) xtitle(Labor income) 

// ** Table 6 and Table 12 (Appendix): Preference shocks with FE and clustering

xi: xtreg TPnewA Event  		   , fe  cluster(HH) 
xi: xtreg TPnewA Event      i.week , fe  cluster(HH) 
xi: xtreg TPnewA EventSpend  	   , fe  cluster(HH) 
xi: xtreg TPnewA EventSpend i.week , fe  cluster(HH) 

// ** True size of the spending effect with IV, FE and clustering 

xi: xtivreg2 TPnewA 		(EventSpend = Event), fe cluster(HH) first savefirst
xi: xtivreg2 TPnewA i.week 	(EventSpend = Event), fe cluster(HH) first savefirst

// ** Table 14: All exogenous and endogenous spending and income, with FE and clustering

xi: xtreg TPnewA EarningsNonLaborEx EventSpend, fe cluster(HH)
xi: xtreg TPnewA EarningsNonLaborEx EventSpend i.week, fe cluster(HH)
xi: xtreg TPnewA EarningsLabor EarningsNonLaborEnd EarningsNonLaborEx EventSpend SpendingNonEvent, fe cluster(HH)
xi: xtreg TPnewA EarningsLabor EarningsNonLaborEnd EarningsNonLaborEx EventSpend SpendingNonEvent i.week, fe cluster(HH)
xi: xtivreg2 TPnewA EarningsLabor EarningsNonLaborEnd EarningsNonLaborEx SpendingNonEvent (EventSpend = Event), fe cluster(HH)
xi: xtivreg2 TPnewA EarningsLabor EarningsNonLaborEnd EarningsNonLaborEx SpendingNonEvent i.week (EventSpend = Event), fe cluster(HH)

// ** Table 15: Quadratic specifications

xi: xtreg TPnewA EarningsLabor EarningsLabor2 ///
 EarningsNonLaborEnd EarningsNonLaborEnd2 EarningsNonLaborEx EarningsNonLaborEx2 ///
 , fe cluster(HH)
test (EarningsNonLaborEx=0)(EarningsNonLaborEx2=0) // Nonlabor income - all coefficients
test (EarningsNonLaborEnd2=0)(EarningsNonLaborEx2=0) (EarningsLabor2=0)  // all quadratic coeff.

xi: xtreg TPnewA EventSpend EventSpend2, fe cluster(HH)
test (EventSpend2=0)(EventSpend=0)  // event spending  - all coefficients
test (EventSpend2=0)  // Event spending quadratic coefficient

xi: xtreg TPnewA  EarningsNonLaborEx EarningsNonLaborEx2 EventSpend EventSpend2 , fe cluster(HH)
test (EarningsNonLaborEx=0)(EarningsNonLaborEx2=0) // Nonlabor income - all coefficients
test (EventSpend2=0)(EventSpend=0)  // event spending  - all coefficients
test (EarningsNonLaborEx2=0) (EventSpend2=0)  // all quadratic

xi: xtreg TPnewA  EarningsLabor EarningsLabor2 EarningsNonLaborEnd EarningsNonLaborEnd2 EarningsNonLaborEx EarningsNonLaborEx2 EventSpend EventSpend2 SpendingNonEvent SpendingNonEvent2, fe cluster(HH)
test (EarningsNonLaborEx=0)(EarningsNonLaborEx2=0) // Nonlabor income - all coefficients
test (EventSpend2=0)(EventSpend=0)  // event spending  - all coefficients
test (EarningsNonLaborEx2=0) (EventSpend2=0)  // all quadratic


// *******************************************
// *Prediction - Savings
// *******************************************

// Table 7: quadratic savings term only
xi: reg   TPdelta Savings Savings2       ,  cluster(HH)
xi: reg   TPdelta Savings Savings2 week3 ,  cluster(HH)
xi: xtreg TPdelta Savings Savings2       , fe cluster(HH)
xi: xtreg TPdelta Savings Savings2 week3 , fe  cluster(HH)

// Table 13: Appendix, savings with 3rd order Taylor expansion
xi: reg TPdelta Savings Savings2 Savings3,  cluster(HH)
est store S21a
xi: reg TPdelta Savings Savings2 Savings3 week3 ,  cluster(HH)
est store S22a
xi: xtreg TPdelta Savings Savings2 Savings3, fe cluster(HH)
est store S23a
xi: xtreg TPdelta Savings Savings2 Savings3 week3, fe  cluster(HH)
est store S24a


// *******************************************
// *Prediction - Spending
// *******************************************

// Table 8
// Total Spending 

xi: xtreg TPnewA Spending, fe cluster(HH)
qui xi: xtreg TPnewA Spending i.week, fe cluster(HH)

// Disaggregated spending

xi: xtreg TPnewA SpendNec EventSpend LgPurchSpend SpendBills SpendGifts SpendFun SpendSocialEvent  , fe cluster(HH)
xi: xtreg TPnewA SpendNec EventSpend LgPurchSpend SpendBills SpendGifts SpendFun SpendSocialEvent i.week, fe cluster(HH)


// ***********************************
// Effect of experimental payments on MRS (not reported in the paper)
xi: xtreg TPnewA pay i.week, fe cluster(HH) 

